package net.zdreamer.shunyi.adapter;

import java.util.List;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.widget.RadioGroup;

import net.zdreamer.shunyi.R;


public class FragmentTabAdapter implements RadioGroup.OnCheckedChangeListener {
	private List<Fragment> fragments;
	private RadioGroup rgs;
	private FragmentActivity fragmentActivity;
	private int fragmentContentId;
	private int currentTab;

	private OnRgsExtraCheckedChangedListener onRgsExtraCheckedChangedListener;
	private boolean hasAnimation;

	public FragmentTabAdapter(FragmentActivity fragmentActivity,
			List<Fragment> fragments, int fragmentContentId, RadioGroup rgs,boolean hasAnimation) {
		this.fragments = fragments;
		this.rgs = rgs;
		this.fragmentActivity = fragmentActivity;
		this.fragmentContentId = fragmentContentId;
		this.hasAnimation=hasAnimation;
            FragmentTransaction ft = fragmentActivity.getSupportFragmentManager()
                    .beginTransaction();
            ft.add(fragmentContentId, fragments.get(0));
            ft.commit();



		rgs.setOnCheckedChangeListener(this);


	}

	public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {
		for (int i = 0; i < rgs.getChildCount(); i++) {
			if (rgs.getChildAt(i).getId() == checkedId) {
				Fragment fragment = fragments.get(i);
				FragmentTransaction ft = obtainFragmentTransaction(i);

				getCurrentFragment().onPause();

				if (fragment.isAdded()) {
					fragment.onResume();
				} else {
					ft.add(fragmentContentId, fragment);
				}
				showTab(i);
				ft.commit();

				if (null != onRgsExtraCheckedChangedListener) {
					onRgsExtraCheckedChangedListener.OnRgsExtraCheckedChanged(
							radioGroup, checkedId, i);
				}

			}
		}

	}

	private void showTab(int idx) {
		for (int i = 0; i < fragments.size(); i++) {
			Fragment fragment = fragments.get(i);
			FragmentTransaction ft = obtainFragmentTransaction(idx);

			if (idx == i) {
				ft.show(fragment);
			} else {
				ft.hide(fragment);
			}
			ft.commit();
		}
		currentTab = idx;
	}

	private FragmentTransaction obtainFragmentTransaction(int index) {
        FragmentTransaction ft;
             ft = fragmentActivity.getSupportFragmentManager()
                    .beginTransaction();


		if(hasAnimation){

			if (index > currentTab) {
				ft.setCustomAnimations(R.anim.slide_left_in, R.anim.slide_left_out);
			} else {
				ft.setCustomAnimations(R.anim.slide_right_in, R.anim.slide_right_out);
			}
		} else {
			if (index > currentTab) {
				ft.setCustomAnimations(0, 0);
			} else {
				ft.setCustomAnimations(0, 0);
			}
		}
		return ft;
	}

	public int getCurrentTab() {
		return currentTab;
	}

	public Fragment getCurrentFragment() {
		return fragments.get(currentTab);
	}

	public OnRgsExtraCheckedChangedListener getOnRgsExtraCheckedChangedListener() {
		return onRgsExtraCheckedChangedListener;
	}

	public void setOnRgsExtraCheckedChangedListener(
			OnRgsExtraCheckedChangedListener onRgsExtraCheckedChangedListener) {
		this.onRgsExtraCheckedChangedListener = onRgsExtraCheckedChangedListener;
	}

	public static interface OnRgsExtraCheckedChangedListener {
		public void OnRgsExtraCheckedChanged(RadioGroup radioGroup,
                                             int checkedId, int index);
	}

}
